﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using log4net;

namespace Biz.Process
{

    public class TriggerInfoBuffer
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(TriggerInfoBuffer));

        private static readonly ConcurrentDictionary<string, TriggerInfo> TheBuffer = new ConcurrentDictionary<string, TriggerInfo>();

        public static void AddInfo(string mn, TriggerInfo info)
        {
            try
            {
                if (TheBuffer.ContainsKey(mn))
                {
                    TriggerInfo i;
                    TheBuffer.TryRemove(mn, out i);
                }
                TheBuffer.TryAdd(mn, info);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        //public static TriggerInfo RemoveInfo(string mn)
        //{
        //    TriggerInfo info = null;
        //    try
        //    {
        //        TheBuffer.TryRemove(mn, out info);
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Error(ex);
        //    }
        //    return info;
        //}

        public static TriggerInfo GetInfo(string mn)
        {
            TriggerInfo info = null;
            try
            {
                TheBuffer.TryGetValue(mn, out info);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return info;
        }


        public static List<string> GetInfoKeys()
        {
            try
            {
                return TheBuffer.Keys.ToList();
            }
            catch (Exception)
            {

                return null;
            }
            
        }
    }
}
